#include "registerdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QRegExpValidator>
#include "utils.h"

RegisterDialog::RegisterDialog(QWidget *parent, SqlOperator *sqlOperator)
    : QDialog(parent), m_sqlOperator(sqlOperator)
{
    setWindowTitle(tr("用户注册"));
    setFixedSize(300, 200);
    setupUI();

}

RegisterDialog::~RegisterDialog()
{
}

void RegisterDialog::setupUI()
{
    // 创建控件
    m_usernameEdit = new QLineEdit(this);
    m_passwordEdit = new QLineEdit(this);
    m_confirmEdit = new QLineEdit(this);
    m_user_nameEdit = new QLineEdit(this);

     // 注册按钮
    m_registerBtn = new QPushButton(tr("注册"), this);
    m_registerBtn->setObjectName("registerBtn"); // 设置对象名用于QSS

    // 取消按钮
    m_cancelBtn = new QPushButton(tr("取消"), this);
    m_cancelBtn->setObjectName("cancelBtn"); // 设置对象名用于QSS

    // 设置密码框属性
    m_passwordEdit->setEchoMode(QLineEdit::Password);
    m_confirmEdit->setEchoMode(QLineEdit::Password);

    // 设置输入验证器，用户名只允许字母、数字和下划线，长度3-20
    QRegExp rx("^[a-zA-Z0-9_]{3,20}$");
    QRegExpValidator *validator = new QRegExpValidator(rx, this);
    m_usernameEdit->setValidator(validator);

    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout();
    formLayout->addRow(tr("用户名:"), m_user_nameEdit);
    formLayout->addRow(tr("账号:"), m_usernameEdit);
    formLayout->addRow(tr("密码:"), m_passwordEdit);
    formLayout->addRow(tr("确认密码:"), m_confirmEdit);

    // 创建按钮布局
    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addWidget(m_registerBtn);
    btnLayout->addWidget(m_cancelBtn);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(btnLayout);
    btnLayout->setSpacing(15); // 增加按钮间距
    setLayout(mainLayout);

    // 创建主布局
    //QVBoxLayout *mainLayout = new QVBoxLayout(this);前面定义过了
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(btnLayout);
    mainLayout->setContentsMargins(20, 20, 20, 20); // 设置边距
    mainLayout->setSpacing(15);

setLayout(mainLayout);

    // 连接信号槽
    connect(m_registerBtn, &QPushButton::clicked, this, &RegisterDialog::onRegisterClicked);
    connect(m_cancelBtn, &QPushButton::clicked, this, &RegisterDialog::onCancelClicked);

}

void RegisterDialog::onRegisterClicked()
{
    QString user_name =m_user_nameEdit->text().trimmed();
    QString username = m_usernameEdit->text().trimmed();
    QString password = m_passwordEdit->text();
    QString confirm = m_confirmEdit->text();

    // 验证输入
    if (username.isEmpty()) {
        QMessageBox::warning(this, tr("输入错误"), tr("账号不能为空"));
        m_usernameEdit->setFocus();
        return;
    }

    if (password.isEmpty()) {
        QMessageBox::warning(this, tr("输入错误"), tr("密码不能为空"));
        m_passwordEdit->setFocus();
        return;
    }

    if (password != confirm) {
        QMessageBox::warning(this, tr("输入错误"), tr("两次输入的密码不一致"));
        m_confirmEdit->setFocus();
        return;
    }

    if (password.length() < 6) {
        QMessageBox::warning(this, tr("输入错误"), tr("密码长度不能少于6位"));
        m_passwordEdit->setFocus();
        return;
    }

    // 检查账号是否已存在
    QList<QVariantList> data;
    QHash<QString, QVariant> where;
    where.insert("account", username);

    if (m_sqlOperator->selectData("user", {"account"}, where, data) && !data.isEmpty()) {
        QMessageBox::warning(this, tr("注册失败"), tr("账号已存在"));
        m_usernameEdit->setFocus();
        return;
    }

    // 密码哈希处理
    QString hashedPassword = Utils::sha256(password);

    // 插入用户数据
    QList<QString> fields;
    fields <<"user_name" << "account" << "password";

    QVariantList values;
    values<<user_name << username << hashedPassword;

    if (m_sqlOperator->insertRowData("user", fields, values)) {
        QMessageBox::information(this, tr("注册成功"), tr("恭喜，注册成功！"));
        accept();
    } else {
        QMessageBox::critical(this, tr("注册失败"),
                             tr("注册失败: %1").arg(m_sqlOperator->lastError()));
    }
}

void RegisterDialog::onCancelClicked()
{
    reject();
}
